package yunwo.cn.yzstation.view.fregment.schedulingcar;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.andview.refreshview.XRefreshView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.adapter.SchedulingAdapter;
import yunwo.cn.yzstation.view.activity.CarDetailsActivity;

/**
 * Created by lao on 2017/9/1.
 */

public class SchedulingFragment extends Fragment {

    private View mRootview;
    private ListView mScheduling_lv;
    private XRefreshView mXrefreshview;
    private SchedulingAdapter mSchedulingAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootview = inflater.inflate(R.layout.fragment_scheduling, container, false);
        return mRootview;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
            initView();
        initEvent();
    }

    private void initEvent() {
        mXrefreshview.setPullRefreshEnable(true);
        mXrefreshview.setAutoLoadMore(false);
        mXrefreshview.setScrollBackDuration(300);
        mXrefreshview.setXRefreshViewListener(new XRefreshListener());
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");list.add("3");list.add("4");list.add("5");
        mSchedulingAdapter = new SchedulingAdapter(getContext());
        mSchedulingAdapter.refreshDataAndNotifyDataSetChanged(list);
        mScheduling_lv.setAdapter(mSchedulingAdapter);
        mScheduling_lv.setOnItemClickListener(new ItemclickListen());
        
    }

    private void initView() {

        mScheduling_lv = (ListView) mRootview.findViewById(R.id.scheduling_lv);
        mXrefreshview = (XRefreshView) mRootview.findViewById(R.id.xrefreshview);
    }

    class ItemclickListen implements AdapterView.OnItemClickListener {

        private Intent mIntent;

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            mIntent = new Intent(getContext(), CarDetailsActivity.class);
            startActivity(mIntent);
        }
    }


    class XRefreshListener extends XRefreshView.SimpleXRefreshListener {
        @Override
        public void onRefresh(boolean isPullDown) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    //模拟数据加载失败的情况
                    Random random = new Random();
                    boolean success = random.nextBoolean();
                    if (success) {
                        Toast.makeText(getContext(),"上拉刷新成功", Toast.LENGTH_SHORT).show();
                        mXrefreshview.stopRefresh();
                    } else {
                        Toast.makeText(getContext(),"失败", Toast.LENGTH_SHORT).show();

                        mXrefreshview.stopRefresh(false);
                    }

                }
            }, 2000);

        }
    }
}
